package com.yiqi.lottery.feature.find.view.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.framework.AM;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.FindScoreBo;
import com.yiqi.lottery.entity.bo.LotteryResultBo;
import com.yiqi.lottery.entity.enums.LotteryType;

import java.util.Arrays;
import java.util.List;

public class LotteryResultAdapter extends BaseQuickAdapter<LotteryResultBo, BaseViewHolder> {

    public LotteryResultAdapter() {
        super(R.layout.item_find_lottery_result);
    }

    @Override
    protected void convert(BaseViewHolder helper, LotteryResultBo item) {
        helper.setText(R.id.tv_find_lottery_result_name, item.getLabel())
                .setText(R.id.tv_find_lottery_result_qi, "第" + item.getIssue() + "期")
                .setText(R.id.tv_find_lottery_result_date, item.getOpentime() + "\t(" + DateTool.getWeek(item.getOpentime()) + ")");

        RecyclerView rvQiu = helper.getView(R.id.rv_lottery_result_qiu);
        rvQiu.setLayoutManager(new GridLayoutManager(AM.app(), 8));
        List<String> qiuData = Arrays.asList(item.getResult2().replaceAll(",", "-").split("-"));
        QiuAdapter qiuAdapter = new QiuAdapter(R.layout.item_find_lottery_result_qiu, qiuData,helper.getAdapterPosition());
        rvQiu.setAdapter(qiuAdapter);


        RecyclerView rvFang = helper.getView(R.id.rv_lottery_result_fang);
        rvFang.setLayoutManager(new GridLayoutManager(AM.app(), 14));
        List<String> fangData = Arrays.asList(item.getResult2().replaceAll(",", "-").split("-"));
        QiuAdapter fangAdapter = new QiuAdapter(R.layout.item_find_lottery_result_fang, fangData,helper.getAdapterPosition());
        rvFang.setAdapter(fangAdapter);

        if (helper.getAdapterPosition() ==0) {
            rvQiu.setVisibility(View.VISIBLE);
            rvFang.setVisibility(View.GONE);
        } else {
            rvQiu.setVisibility(View.GONE);
            rvFang.setVisibility(View.VISIBLE);
        }
    }

    class QiuAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        int position = 0;

        public QiuAdapter(int layoutResId, @Nullable List<String> data, int position) {
            super(layoutResId, data);
            this.position = position;
        }


        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.tv_result_qiu, item);
            TextView textView = helper.getView(R.id.tv_result_qiu);
            if (position == LotteryType.大乐透.getIndex()) {
                if (helper.getAdapterPosition() > 4) {
                    textView.setBackgroundResource(R.drawable.documentary_list_first_blue);
                }
            }
        }
    }
}
